package com.itheima.rpc.client.cluster.strategy;

import com.itheima.rpc.annotation.HrpcLoadBalance;
import com.itheima.rpc.client.cluster.LoadBalanceStrategy;
import com.itheima.rpc.client.cluster.StrategyProvider;
import com.itheima.rpc.client.config.RpcClientConfiguration;
import com.itheima.rpc.spring.SpringBeanFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;


@Slf4j
@Component
public class DefaultStrategyProvider implements StrategyProvider {

    @Resource
    private RpcClientConfiguration configuration;

    @Override
    public LoadBalanceStrategy getStrategy() {
        Map<String, Object> map = SpringBeanFactory.getBeanListByAnnotationClass(HrpcLoadBalance.class);
        for (Object bean : map.values()) {
            HrpcLoadBalance annotation = bean.getClass().getAnnotation(HrpcLoadBalance.class);
            if (configuration.getRpcClientClusterStrategy().equals(annotation.strategy())) {
                return (LoadBalanceStrategy)bean;
            }
        }
        return null;
    }
}
